﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Networking.Sockets;
using Windows.Networking;
using Windows.Storage.Streams;

namespace Touch_Pad.Giang.Phan.Libs.Networks
{
    class G_Socket
    {
        const int ECHO_PORT = 7;
        const int QOTD_PORT = 17;
        const int TIMEOUT_MILLISECONDS = 5000;
        const int MAX_BUFFER_SIZE = 2048;
        StreamSocket socket;
        DataWriter writer;
        public G_Socket()
        {
            this.socket = new StreamSocket();

        }

        public async void connect(String remote, String port)
        {
            if (this.socket == null)
                throw new Exception("soket không tồn tại");
            HostName remoteHost = new HostName(remote);
            await this.socket.ConnectAsync(remoteHost, port);
        }

        public async void sendData(String msg)
        {
            this.writer = new DataWriter(this.socket.OutputStream);
            this.writer.WriteUInt32(this.writer.MeasureString(msg));
            this.writer.WriteString(msg);
            try
            {
                await this.writer.StoreAsync();
            }
            catch (Exception ex)
            {

            }

        }


    }
}
